<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use think\Db;
use think\facade\Config;
use addons\epay\library\Service;

class Wxpay extends Frontend {

    /**
     * 公众号支付 必须以get形式传递 out_trade_no 参数
     * 示例请看 /Application/Home/Controller/IndexController.class.php
     * 中的wexinpay_js方法
     */
    public function pay() {
        $out_trade_no = $this->request->param('out_trade_no');

        $pay = Service::createPay('wechat');

        $orderData = db('order')->where(['order_no' => $out_trade_no])->find();

        if ($orderData['status'] != 0) {
            $this->redirect(url('My/index'));
        }
        $order_type = $orderData['type'];
        //构建订单信息
        $order = [
            'out_trade_no' => $out_trade_no, //你的订单号
            'total_fee' => $orderData['real_price'] * 100, //单位分
            'body' => '魔盒便利充值',
        ];


        $order['openid'] = db('users')->where(['user_id' => $this->iUserId])->value('wx_openid');

        $data = $pay->mp($order);

        $assign = array(
            'data' => json_encode($data)
        );

        $url = url('My/index');

        if ($order_type != 10) {
            $url = url('Order/payStatus', array('out_trade_no' => $out_trade_no));
        }
        $this->assign($assign);
        $this->assign('user_box_id', $this->userBoxId);
        $this->assign('jump_url', $url);
        $this->assign('order_id', $orderData['order_id']);
        $this->assign('order_type', $order_type);
        return $this->view->fetch();
    }

}
